package com.zhipu.resetbalance.ui.home.system.bh;

import android.os.Bundle;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;
import com.zhipu.resetbalance.R;
import com.zhipu.resetbalance.databinding.ActivityNumberManagementBinding;
import com.zhipu.resetbalance.listener.ChooseClickCallback;
import com.zhipu.resetbalance.listener.ChooseClickCallback1;
import com.zhipu.resetbalance.mobile.dao.NumberedEntityDao;
import com.zhipu.resetbalance.mobile.event.NumberedEntity;
import com.zhipu.resetbalance.pop.AddNumberPopup;
import com.zhipu.resetbalance.pop.SelectModifyDeletePopup;
import com.zhipu.resetbalance.ui.BaseActivity;
import com.zhipu.resetbalance.ui.home.system.bh.adapter.NumberManagementAdapter;

import java.util.List;


/**
 * 编号管理
 */
public class NumberManagementActivity extends BaseActivity<ActivityNumberManagementBinding> implements ChooseClickCallback {


    /**
     * VM
     */
    private NumberManagementViewModel mNumberManagementViewModel;


    /**
     * AP
     */
    private NumberManagementAdapter mNumberManagementAdapter;

    /**
     *
     */
    private List<NumberedEntity> mNumberedEntityList;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_number_management;
    }



    @Override
    public void initViewModel() {
        super.initViewModel();
        //VM
        mNumberManagementViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(NumberManagementViewModel.class);
        binding.setViewModel(mNumberManagementViewModel);

    }

    @Override
    public void initView() {
        super.initView();
        mNumberManagementAdapter = new NumberManagementAdapter();
        mNumberManagementAdapter.setChooseClickCallback(this);
        binding.rv.setAdapter(mNumberManagementAdapter);
        binding.rv.setLayoutManager(new LinearLayoutManager(mContext));
    }


    @Override
    public void initData() {
        super.initData();
        if(null != mNumberManagementAdapter){
            mNumberedEntityList = NumberedEntityDao.getInstance().getAllList();
            mNumberManagementAdapter.setNumberedEntityList(mNumberedEntityList);
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();


    }

    @Override
    public void onChooseClickCallback(int mInt) {
        NumberedEntity mNumberedEntity = mNumberedEntityList.get(mInt);
        if(null != mNumberedEntity){
            SelectModifyDeletePopup mSelectModifyDeletePopup = new SelectModifyDeletePopup(mContext);
            mSelectModifyDeletePopup.setChooseClickCallback(new ChooseClickCallback() {
                @Override
                public void onChooseClickCallback(int mInt) {
                    switch (mInt){
                        case 0:
                            for(NumberedEntity numberedEntity : mNumberedEntityList){
                                if(mNumberedEntity.NumberedEntityId == numberedEntity.NumberedEntityId){
                                    /**
                                     * 选中
                                     */
                                    numberedEntity.select = true;
                                }else{
                                    /**
                                     * 选中
                                     */
                                    numberedEntity.select = false;
                                }
                                /**
                                 * 创建时间
                                 */
                                numberedEntity.creationTime = TimeUtils.getNowMills();
                                /**
                                 * 添加
                                 */
                                numberedEntity.update();
                            }
                            //刷新页面
                            initData();
                            break;
                        case 1:
                            AddNumberPopup mAddNumberPopup = new AddNumberPopup(ActivityUtils.getTopActivity(),mNumberedEntity);
                            mAddNumberPopup.setChooseClickCallback(new ChooseClickCallback1() {
                                @Override
                                public void onChooseClickCallback(String mData) {
                                    //
                                    mNumberedEntity.NumberedName = mData;
                                    //
                                    mNumberedEntity.creationTime = TimeUtils.getNowMills();
                                    //
                                    mNumberedEntity.update();
                                }
                            });
                            new XPopup
                                    .Builder(ActivityUtils.getTopActivity())
                                    .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                                    .asCustom(mAddNumberPopup)
                                    .show();
                            break;
                        case 2:
                            if(mNumberedEntity.delete()){
                                //刷新页面
                                initData();
                            }
                            break;
                    }
                }
            });
            new XPopup
                    .Builder(ActivityUtils.getTopActivity())
                    .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                    .asCustom(mSelectModifyDeletePopup)
                    .show();
        }
    }
}
